/*
 * @Author: Zeratul
 * @Date: 2020-08-26 08:24:49
 * @LastEditTime: 2020-08-27 08:26:36
 * @FilePath: \sz-h5-2003-react-mobile\zhangminhui\eleme-react\eleme-user\src\store\reducer\orderDetailReducer\index.js
 */
import { ADDIMG, ADDTEXT, ADDFOODSORCE, ADDHORCECOM } from "../../actionType";
import axios from "../../../utils/myaxios"
const defaultState = {
    
    
    orderDetail:{
        orderId:1339850932846023,
        shop:"三顾冒菜",
        imgUrl:require("../../../assets/images/sangumaocai.jpg"),
        discounts:3,
        goods:[
            {id:"11",
            name:"女神单人套餐女神单人套餐",
            price:19.9,originPrice:35,
            imgUrl:require("../../../assets/images/nvshentaocan.jpg"),
            specification:"精致",
            num:1,
            },
            {id:"12",
            name:"蜂蜜柠檬冷饮",
            price:6.99,
            originPrice:12,
            imgUrl:require("../../../assets/images/ningmengshui.jpg"),
            specification:"少冰",
            num:1,
        }
            ],
        packExpense:2,
        deliveryCost:2,
        forwardingTime:"尽快送达",
        shopppingAdress:"深圳市宝安区航程街道后瑞社区金铂寓",
        orderStatus:"已送达",
        payment:28.9,
        deliveryTime:"尽快送达",
        deliveryMethod:"吃了么专送",
        horseman:{id:"h1",name:"梁某某",tel:"13811111111"},
        payMethod:"在线支付",
        orderTime:"2020-8-26 11:56:00",
    },
    ordercomment:{
        imgfiles:[],
        text:"",
        foodSorce:0,
        horsemanComment:""
    },
    userName:"",
    stars:[
    {star:"#icon-xingxing2",id:"star1"},
    {star:"#icon-xingxing2",id:"star2"},
    {star:"#icon-xingxing2",id:"star3"},
    {star:"#icon-xingxing2",id:"star4"},
    {star:"#icon-xingxing2",id:"star5"},
    ],
    smiles:[
        {face:"#icon-nanguo",id:"face1",text:"非常差"},
        {face:"#icon-Smile",id:"face2",text:"一般"},
        {face:"#icon-daxiao",id:"face3",text:"超赞"}
    ],
    isrefer:false
}

export default function(state=defaultState,action){
    let newState = JSON.parse(JSON.stringify(state));
    switch(action.type){
        case ADDIMG:
            newState.ordercomment.imgfiles=action.payload.file;
            upload(newState.ordercomment.imgfiles)
        break;
        case ADDTEXT:
            newState.ordercomment.text=action.payload.text;
        break;
        case ADDFOODSORCE:
            let index
            newState.stars.forEach((v,i)=>{
                if(action.payload.id === v.id){
                    index=i
                }
            })
            newState.stars = newState.stars.map((v,i)=>{
                if(i<=index){
                    v.star = "#icon-xingxing1"
                }else{
                    v.star = "#icon-xingxing2"
                }
                return v;
            })
            newState.ordercomment.foodSorce = index+1;
        break;
        case ADDHORCECOM:
            let idx = action.payload.index;
            newState.smiles.map((v,i)=>{
                if(i<=idx){
                    v.face =  v.face.indexOf("-copy")===-1?v.face +"-copy":v.face;
                }else{
                    v.face = v.face.indexOf("-copy") === -1? v.face : v.face.slice(0,v.face.length-5)
                }
                return v;
            })
            newState.ordercomment.horsemanComment = newState.smiles[idx].text;
        break;
        default:
            break;
    }
    if(newState.ordercomment.imgfiles.length>0 
        && newState.ordercomment.text 
        && newState.ordercomment.foodSorce>0 
        && newState.ordercomment.horsemanComment){
            newState.isrefer = true;
        }else{
        newState.isrefer = false;
    }
    return newState;
}

const upload = (files) => {
    for (let i = 0; i < files.length; i++) {
        let formData = new FormData();
        let file = files[i].file;
        formData.append('images', file);
        formData.append('path', "./uploads")   // 这里根据自己项目后台实际情况定
        var url = 'http://10.36.149.43:10800/api/user/uploadsCommits';
        axios.post(url, formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }).then((json) => {
          console.log(json); 
        }).catch();
    }
}